package com.dfec.water.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dfec.common.exception.ServiceException;
import com.dfec.common.utils.AjaxResult;
import com.dfec.component.uid.core.service.UidService;
import com.dfec.framework.mybatis.core.page.PageParam;
import com.dfec.framework.mybatis.core.page.PageUtils;
import com.dfec.water.dao.*;
import com.dfec.water.entity.YdbAreaDeviceEntity;
import com.dfec.water.entity.YdbDeviceObserveEntity;
import com.dfec.water.entity.YdbDeviceTypeEntity;
import com.dfec.water.entity.YdbDeviceZsyEntity;
import com.dfec.water.service.YdbDeviceTypeService;
import com.dfec.water.util.TableUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author df
 * @title: YdbDeviceTypeServiceImpl
 * @description: 设备类别Service业务层处理
 * @date Fri Jan 10 15:02:12 CST 2025
 */
@Service("ydbDeviceTypeService")
@RequiredArgsConstructor
public class YdbDeviceTypeServiceImpl extends ServiceImpl<YdbDeviceTypeDao, YdbDeviceTypeEntity> implements YdbDeviceTypeService {

    private final YdbDeviceTypeDao ydbDeviceTypeDao;

    private final UidService uidService;

    private final YdbAreaDeviceDao areaDeviceDao;

    @Resource
    private YdbDeviceObserceDao ydbDeviceObserceDao;

    @Resource
    private YdbDeviceZsyDao deviceZsyDao;

    @Resource
    private YdbAreaBaseInfoDao areaBaseInfoDao;

    /**
     * 分页查询
     *
     * @param pageParam 入参
     * @return PageUtils
     * @author df
     * @date Fri Jan 10 15:02:12 CST 2025
     */
    @Override
    public PageUtils queryPage(PageParam<YdbDeviceTypeEntity> pageParam) {
        return ydbDeviceTypeDao.selectPage(pageParam);
    }

    /**
     * 不带分页查询
     *
     * @param ydbDeviceTypeEntity 入参
     * @return List<YdbDeviceTypeEntity>
     * @author df
     * @date Fri Jan 10 15:02:12 CST 2025
     */
    @Override
    public List<YdbDeviceTypeEntity> listAll(YdbDeviceTypeEntity ydbDeviceTypeEntity) {
        return ydbDeviceTypeDao.selectAll(ydbDeviceTypeEntity);
    }


    @Override
    public boolean save(YdbDeviceTypeEntity entity) {
        entity.setId(uidService.next());
        return super.save(entity);
    }

    @Override
    public boolean removeByIds(Collection<?> list) {
        //校验当前设备是否已经绑定
        for (Object id : list) {
            YdbAreaDeviceEntity ydbAreaDeviceEntity = new YdbAreaDeviceEntity();
            ydbAreaDeviceEntity.setDeviceId(Long.parseLong(id.toString()));
            List<YdbAreaDeviceEntity> ydbAreaDeviceEntities = areaDeviceDao.selectAll(ydbAreaDeviceEntity);
            if (!ydbAreaDeviceEntities.isEmpty()) {
                throw new ServiceException("当前设备已经绑定区域，无法删除");
            }
        }

        return super.removeByIds(list);
    }

    @Override
    public boolean saveBatch(Collection<YdbDeviceTypeEntity> entityList) {
        entityList.forEach(entity -> entity.setId(uidService.next()));
        return super.saveBatch(entityList);
    }


    @Override
    public AjaxResult getDeviceData(Long deviceId, Integer deviceType, Integer pageNo, Integer pageSize) {
        Map<String, Object> outMap = new HashMap<>(2);
        int gcNum = 6, zty = 7;
        if (deviceType == gcNum) {
            List<Map<Object, Object>> maps = areaBaseInfoDao.selectTableInfo("ydb_device_observer");
            outMap.put("tableInfo", dealJavaField(maps));
            //观测设备
            YdbDeviceObserveEntity ydbDeviceObserveEntity = new YdbDeviceObserveEntity();
            ydbDeviceObserveEntity.setDeviceId(deviceId);
            Page page = new Page<>(pageNo, pageSize);
            Page page1 = ydbDeviceObserceDao.selectPage(page, ydbDeviceObserceDao.dealParams(ydbDeviceObserveEntity));
            PageUtils pageUtils = new PageUtils(page1);
            outMap.put("data", pageUtils);
            return AjaxResult.ok().put(outMap);
        } else if (deviceType == zty) {
            List<Map<Object, Object>> maps = areaBaseInfoDao.selectTableInfo("ydb_device_zsy");
            outMap.put("tableInfo", dealJavaField(maps));
            //渗透仪设备
            YdbDeviceZsyEntity ydbDeviceZsyEntity = new YdbDeviceZsyEntity();
            ydbDeviceZsyEntity.setDeviceId(deviceId);
            Page page = new Page<>(pageNo, pageSize);
            Page page1 = deviceZsyDao.selectPage(page, deviceZsyDao.dealParams(ydbDeviceZsyEntity));
            PageUtils pageUtils = new PageUtils(page1);
            outMap.put("data", pageUtils);

            return AjaxResult.ok().put(outMap);
        }
        return AjaxResult.ok().put(Collections.emptyList());
    }

    public List<Map<Object, Object>> dealJavaField(List<Map<Object, Object>> list) {
        list.forEach(map -> {
            String columnName = TableUtils.toCamelCase(map.get("column_name").toString());
            map.put("column_name", columnName);
        });
        return list;
    }

}